package com.study.controller.user;

import com.study.dto.ActivityCollectionListPageQueryDTO;
import com.study.dto.ActivitySearchDTO;
import com.study.result.PageResult;
import com.study.result.Result;
import com.study.service.ActivityService;
import com.study.vo.ActivityDetailVO;
import com.study.vo.ActivityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/activity")
@Slf4j
@Api(tags = "活动相关接口")
public class ActivityController {

    @Autowired
    ActivityService activityService;

    /**
     * 条件获取活动列表
     *
     * @param activitySearchDTO
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "条件获取活动列表")
    public Result<List<ActivityVO>> getActivityList(@RequestBody ActivitySearchDTO activitySearchDTO) {
        log.info("获取活动列表:{}", activitySearchDTO);
        List<ActivityVO> activities = activityService.getActivityList(activitySearchDTO);
        return Result.success(activities);
    }

    /**
     * 获取用户收藏的活动列表
     *
     * @param activityCollectionListPageQueryDTO
     * @return
     */
    @PostMapping("/collection/list")
    @ApiOperation(value = "获取用户收藏的活动列表")
    public Result<PageResult> getCollectionActivityList(@RequestBody ActivityCollectionListPageQueryDTO activityCollectionListPageQueryDTO) {
        log.info("获取用户收藏的活动列表:{}", activityCollectionListPageQueryDTO);
        PageResult pageResult = activityService.getCollectionActivityListByUserId(activityCollectionListPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 收藏活动
     * @param activityId
     * @return
     */
    @PostMapping("/collection/{activityId}")
    @ApiOperation(value = "收藏活动")
    public Result<String> collectionActivity(@PathVariable Long activityId) {
        activityService.collectionActivity(activityId);
        return Result.success("收藏成功");
    }

     //取消收藏活动
    @DeleteMapping("/cancelCollection/{activityId}")
    @ApiOperation(value = "取消收藏")
    public Result<String> cancelCollectionActivity(@PathVariable Long activityId) {
        activityService.cancelCollectionActivity(activityId);
        return Result.success("取消收藏成功");
    }

    /**
     * 根据活动id获取活动详情
     * @param activityId
     * @return
     */
    @GetMapping("/getActivityDetail/{activityId}")
    @ApiOperation(value = "根据活动id获取活动详情")
    @Cacheable(cacheNames = "activityDetailCache", key = "#activityId") //key: activityDetailCache::1
    public Result<ActivityDetailVO> getActivityById(@PathVariable Long activityId) {
        ActivityDetailVO activityDetailVO = activityService.getActivityById(activityId);
        return Result.success(activityDetailVO);
    }

    /**
     * 根据活动id和用户id判断用户是否收藏了该活动
     * @param userId
     * @param activityId
     * @return
     */
    @GetMapping("/isCollection/")
    @ApiOperation(value = "根据活动id和用户id判断用户是否收藏了该活动")
    public Result<Boolean> isCollection(Long userId, Long activityId) {
        return Result.success(activityService.isCollectionByUserId(userId, activityId));
    }
}
